new Vue({
    el: '#app',
    data() {
        return {
            tableData: [],
            form: {
                page: 1,
                limit: 15,
            },
            loading: false,
            success: 0,
            error: 0
        }
    },
    mixins:[importMixin],
    mounted() {

    },
    methods: {
        submitUpload(i = 0) {
            if (i < this.tableData.length) {
                this.loading = true;
                if (!this.tableData[i].Department_SerialNumber) {
                    this.tableData[i].isStatus = '2';
                    this.tableData[i].Cause = '所属部门编号不能为空';
                    this.error++;
                    this.submitUpload(i + 1);
                }else{
                    let arr = this.tableData[i].Department_SerialNumber.split(';');
                    console.log(arr);
                    let obj = {
                        Member_SerialNumber: this.tableData[i].Member_SerialNumber,
                        Member_Name: this.tableData[i].Member_Name,
                        Member_Notes: '',
                        Email: this.tableData[i].Email,
                        Image: '',
                        Sex: this.tableData[i].Sex == '男' ? '0' : '1',
                        Department_SerialNumber: JSON.stringify([arr]),
                        Creation_Datetime:getYMDHMSDate()
                    };
                    this.submitForm({ Data: JSON.stringify({ Add: { business_member_info: [obj] } }) }, (res) => {
                        if (res.Status == 'success') {
                            this.tableData[i].isStatus = '1';
                            this.success++;
                        } else {
                            this.tableData[i].isStatus = '2';
                            this.tableData[i].Cause = '该用户已注册！';
                            this.error++;
                        }
                        this.submitUpload(i + 1);
                    })
                }
            } else if (i != 0) {
                this.loading = false;
                this.$message.success("批量导入结束")
            } else {
                this.$message({
                    message: '请先导入模板后再上传',
                    type: 'warning'
                })
            }
        },
        submitForm(data, callback) {
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalSave`,
                type: 'POST',
                data,
                success: function (res) {
                    callback(res);
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            })
        },
        handleChange({ raw }) {//导入监听文件
            const that = this;
            var reader = new FileReader();
            reader.readAsBinaryString(raw);
            reader.onload = function (e) {
                var data = e.target.result;
                var wb = XLSX.read(data, {
                    type: 'binary' //以二进制的方式读取
                });
                var sheet = wb.Sheets[wb.SheetNames[0]];//sheet0代表excel表格中的第一页
                var json = XLSX.utils.sheet_to_json(sheet);//利用接口实现转换。
                let arr = [];
                json.forEach(item => {
                    let Member_SerialNumber = item['手机号']?item['手机号']:'',
                    Member_Name = item['姓名']?item['姓名']:'',
                    Email = item['邮箱']?item['邮箱']:'',
                    Sex = item['性别']? item['性别'] : '男',
                    Department_SerialNumber = item['所属部门编号']? item['所属部门编号'] : ''
                    arr.push({
                        Member_SerialNumber,
                        Member_Name,
                        Email,
                        Sex,
                        Department_SerialNumber,
                        isEdit:false,
                        isStatus:'0',
                        Cause:''
                    })
                });
                that.tableData = arr
            }
        },
        handleSizeChange(val) {//方法
            this.form.page = 1;
            this.form.limit = val;
        },
        handleCurrentChange(val) {//换页
            this.form.page = val;
        },
        handleEdit(row) {//修改当前行
            row.isEdit = !row.isEdit
            console.log(row);
        },
        handleRemove(index) {//移除当前行
            this.$confirm('是否要删除此行?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                this.tableData = this.tableData.filter((i, id) => id != (this.form.page - 1) * this.form.limit + index)
            }).catch(() => { });
        },
        handleClean() {//清空
            this.$confirm('是否要清空导入数据?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                this.tableData = []
            }).catch(() => { });
        },
    },
})